package cn.lsp.sharkmall.controller;

import cn.lsp.sharkmall.common.ServiceResultEnum;
import cn.lsp.sharkmall.config.annotation.TokenToUser;
import cn.lsp.sharkmall.pojo.dto.SaveUserAddressDTO;
import cn.lsp.sharkmall.pojo.dto.UpdateUserAddressDTO;
import cn.lsp.sharkmall.pojo.entity.User;
import cn.lsp.sharkmall.pojo.entity.UserAddress;
import cn.lsp.sharkmall.pojo.vo.UserAddressVO;
import cn.lsp.sharkmall.service.IUserAddressService;
import cn.lsp.sharkmall.util.BeanUtil;
import cn.lsp.sharkmall.util.Result;
import cn.lsp.sharkmall.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Author: sweeterjava@163.com
 * @Date: 2023/2/13
 * @Time: 02:58
 */

@RestController
@Api(value = "v1", tags = "6.个人地址相关接口")
@RequestMapping("/api/v1")
public class UserAddressController {
    @Resource
    private IUserAddressService userAddressService;

    @GetMapping("/address")
    @ApiOperation(value = "我的收货地址列表", notes = "")
    public Result<List<UserAddressVO>> addressList(@TokenToUser User loginMallUser) {
        return ResultGenerator.genSuccessResult(
                userAddressService.getMyAddresses(loginMallUser.getUserId()));
    }

    @PostMapping("/address")
    @ApiOperation(value = "添加地址", notes = "")
    public Result<Boolean> saveUserAddress(@RequestBody SaveUserAddressDTO saveMallUserAddressParam,
                                           @TokenToUser User loginMallUser) {
        UserAddress userAddress = new UserAddress();
        BeanUtil.copyProperties(saveMallUserAddressParam, userAddress);
        userAddress.setUserId(loginMallUser.getUserId());
        Boolean saveResult = userAddressService.saveUserAddress(userAddress);
        //添加成功
        if (saveResult) {
            return ResultGenerator.genSuccessResult();
        }
        //添加失败
        return ResultGenerator.genFailResult("添加失败");
    }

    @PutMapping("/address")
    @ApiOperation(value = "修改地址", notes = "")
    public Result<Boolean> updateMallUserAddress(@RequestBody UpdateUserAddressDTO updateUserAddressParam,
                                                 @TokenToUser User loginMallUser) {
        UserAddress mallUserAddressById =
                userAddressService.getUserAddressById(updateUserAddressParam.getAddressId());
        if (!loginMallUser.getUserId().equals(mallUserAddressById.getUserId())) {
            return ResultGenerator.genFailResult(ServiceResultEnum.REQUEST_FORBIDEN_ERROR.getResult());
        }
        UserAddress userAddress = new UserAddress();
        BeanUtil.copyProperties(updateUserAddressParam, userAddress);
        userAddress.setUserId(loginMallUser.getUserId());
        Boolean updateResult = userAddressService.updateUserAddress(userAddress);
        //修改成功
        if (updateResult) {
            return ResultGenerator.genSuccessResult();
        }
        //修改失败
        return ResultGenerator.genFailResult("修改失败");
    }

    @GetMapping("/address/{addressId}")
    @ApiOperation(value = "获取收货地址详情", notes = "传参为地址id")
    public Result<UserAddressVO> getMallUserAddress(@PathVariable("addressId") Long addressId,
                                                    @TokenToUser User loginMallUser) {
        UserAddress mallUserAddressById = userAddressService.getUserAddressById(addressId);
        UserAddressVO newBeeMallUserAddressVO = new UserAddressVO();
        BeanUtil.copyProperties(mallUserAddressById, newBeeMallUserAddressVO);
        if (!loginMallUser.getUserId().equals(mallUserAddressById.getUserId())) {
            return ResultGenerator.genFailResult(ServiceResultEnum.REQUEST_FORBIDEN_ERROR.getResult());
        }
        return ResultGenerator.genSuccessResult(newBeeMallUserAddressVO);
    }

    @GetMapping("/address/default")
    @ApiOperation(value = "获取默认收货地址", notes = "无传参")
    public Result getDefaultMallUserAddress(@TokenToUser User loginMallUser) {
        UserAddress mallUserAddressById =
                userAddressService.getMyDefaultAddressByUserId(loginMallUser.getUserId());
        return ResultGenerator.genSuccessResult(mallUserAddressById);
    }

    @DeleteMapping("/address/{addressId}")
    @ApiOperation(value = "删除收货地址", notes = "传参为地址id")
    public Result deleteAddress(@PathVariable("addressId") Long addressId,
                                @TokenToUser User loginMallUser) {
        UserAddress mallUserAddressById = userAddressService.getUserAddressById(addressId);
        if (!loginMallUser.getUserId().equals(mallUserAddressById.getUserId())) {
            return ResultGenerator.genFailResult(ServiceResultEnum.REQUEST_FORBIDEN_ERROR.getResult());
        }
        Boolean deleteResult = userAddressService.deleteById(addressId);
        //删除成功
        if (deleteResult) {
            return ResultGenerator.genSuccessResult();
        }
        //删除失败
        return ResultGenerator.genFailResult(ServiceResultEnum.OPERATE_ERROR.getResult());
    }
}
